<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : User Model
 * @description : Database functions related to users data
 * 
 */
class User_model extends CI_Model {

  function __construct() {
	parent::__construct();
  }

  function set_user($user_name = NULL, $enc_password = NULL, $work_email = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_name) && !empty($enc_password) && !empty($work_email)) {
	  $query_insr = array(
		  'user_name' => trim($user_name),
		  'user_display_name' => ucwords($user_name),
		  'user_password' => $enc_password,
		  'user_work_email' => $work_email,
		  'user_status' => 0
	  );
	  $query_exec = $this->db->insert('users', $query_insr);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result']['user_id'] = $this->db->insert_id();
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function get_user_by_password($user_name = NULL, $enc_password = NULL, $user_status = 1) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_name) && !empty($enc_password)) {
	  $query_cond = array();
	  if (filter_var($user_name, FILTER_VALIDATE_EMAIL)) {
		$query_cond['user_work_email'] = $user_name;
	  } else {
		$query_cond['user_name'] = $user_name;
	  }
	  $query_cond['user_password'] = $enc_password;
	  $query_cond['user_status'] = $user_status;
	  $query_cond['user_resigned'] = 0;
	  $query_exec = $this->db->get_where('users', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->row_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  function get_user_by_id($user_id = NULL, $user_status = 1) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_id) && is_numeric($user_id)) {
	  $query_cond = array();
	  $query_cond['user_id'] = $user_id;
	  $query_cond['user_status'] = $user_status;
	  $query_exec = $this->db->get_where('users', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->row_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  function get_all_users($limit = NULL, $offset = NULL, $status = array()) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_is_user'] = 1;
	$query_cond['user_status'] = 1;
	$this->db->order_by('user_likes_level', 'desc');
	$this->db->order_by('user_resigned', 'asc');
	$query_exec = $this->db->get_where('users', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  function get_all_non_users($status = 1, $limit = null) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	if (!empty($status) && is_numeric($status)) {
	  $query_cond['user_status'] = $status;
	} else {
	  $query_cond['user_status'] = 1;
	}
	$query_cond['user_is_user'] = 0;
	$query_exec = $this->db->get_where('users', $query_cond, $limit);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

  function get_user_by_username($username = NULL, $check_status = TRUE) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($username)) {
	  $query_cond = array();
	  $query_cond['user_name'] = $username;
	  if ($check_status == TRUE) {
		$query_cond['user_status'] = 1;
	  }
	  $query_exec = $this->db->get_where('users', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->row_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  function update_password($user_id = NULL, $enc_password = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_id) && is_numeric($user_id) && !empty($enc_password)) {
	  $query_exec = $this->db->update('users', array('user_password' => $enc_password), array('user_id' => $user_id));
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $this->db->affected_rows();
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function update_profile($user_id = NULL, $profile_data = array()) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_id) && is_numeric($user_id) && !empty($profile_data) && is_array($profile_data)) {
	  $query_exec = $this->db->update('users', $profile_data, array('user_id' => $user_id));
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $this->db->affected_rows();
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function get_user_search($user_name = NULL) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_name)) {
	  $query_cond = array();
	  $query_cond['user_status'] = 1;
	  $this->db->like('user_name', $user_name, 'after');
	  $this->db->or_like('user_display_name', $user_name, 'after');
	  $this->db->order_by('user_name', 'asc');
	  $query_exec = $this->db->get_where('users', $query_cond, 10);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->result_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  function update_online($user_id = NULL, $go_offline = FALSE) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_id) && is_numeric($user_id)) {
	  $query_upda = array();
	  if ($go_offline) {
		$query_upda['user_online_date'] = "0000-00-00 00:00:00";
	  } else {
		$query_upda['user_online_date'] = date("Y-m-d H:i:s");
	  }
	  $query_exec = $this->db->update('users', $query_upda, array('user_id' => $user_id));
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $this->db->affected_rows();
		$result_data['count'] = 1;
	  }
	}
	return $result_data;
  }

  function get_users_by_ids($user_ids = array(), $status = array()) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_ids) && is_array($user_ids)) {
	  $query_cond = array();
	  if (!empty($status) && is_array($status)) {
		foreach ($status as $status) {
		  if (!empty($status) && is_numeric($status)) {
			$this->db->or_where('user_status =', $status);
		  }
		}
	  } else {
		$query_cond['user_status'] = 1;
	  }
	  $this->db->select('user_id, user_name, user_display_name, user_image');
	  $this->db->order_by('user_id', 'RANDOM');
	  $this->db->where_in('user_id', $user_ids);
	  $query_exec = $this->db->get_where('users', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->result_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  function get_users_by_usernames($user_names = array(), $status = array()) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	if (!empty($user_names) && is_array($user_names)) {
	  $query_cond = array();
	  if (!empty($status) && is_array($status)) {
		foreach ($status as $status) {
		  if (!empty($status) && is_numeric($status)) {
			$this->db->or_where('user_status =', $status);
		  }
		}
	  } else {
		$query_cond['user_status'] = 1;
	  }
	  $this->db->select('user_id, user_name, user_display_name, user_image');
	  $this->db->where_in('user_name', $user_names);
	  $query_exec = $this->db->get_where('users', $query_cond);
	  if ($query_exec == TRUE) {
		$result_data = array();
		$result_data['status'] = 1;
		$result_data['result'] = $query_exec->result_array();
		$result_data['count'] = $query_exec->num_rows();
	  }
	}
	return $result_data;
  }

  function get_online_users($limit = NULL, $offset = NULL, $status = array()) {
	$result_data = array();
	$result_data['status'] = 0;
	$result_data['result'] = '';
	$result_data['count'] = 0;
	$query_cond = array();
	$query_cond['user_is_user'] = 1;
	$query_cond['user_status'] = 1;
	$this->db->order_by('user_online_date', 'desc');
	$this->db->order_by('user_resigned', 'asc');
	$query_exec = $this->db->get_where('users', $query_cond, $limit, $offset);
	if ($query_exec == TRUE) {
	  $result_data = array();
	  $result_data['status'] = 1;
	  $result_data['result'] = $query_exec->result_array();
	  $result_data['count'] = $query_exec->num_rows();
	}
	return $result_data;
  }

}

?>
